package cn.itcast.service.system.impl;

import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.MailUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }

//    @Autowired
//    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AmqpTemplate amqpTemplate;


    @Override
    public void save(User user) {
//        String password = user.getPassword(); //明文的
        String password = RandomStringUtils.random(6,"UTF-8"); //随机产生的密码
//        对密码加密  Object source 原密码, Object salt 盐, int hashIterations 散次列 加几次 但是这个加的次数超过10 效率就比较低
        String password_miwen = new Md5Hash(password,user.getEmail(),2).toString();
        user.setPassword(password_miwen); //把加密后的密码再放回到对象中
        userDao.save(user);
//        发送一个短信
//        发送邮件
//           MailUtil.sendMsg(user.getEmail(),"恭喜成为SaaS货代云平台会员","您的密码是："+password+",请妥善保存");
//        把发送邮件的任务放到MQ中
        Map<String,String> map = new HashMap();
        map.put("to",user.getEmail());
        map.put("subject","恭喜成为SaaS货代云平台会员");
        map.put("content","您的密码是："+password+",请妥善保存");
        amqpTemplate.convertAndSend("user.insert",map);

    }

    @Override
    public User findById(String id) {
        return  userDao.findById(id);
    }

    @Override
    public void update(User user) {
         userDao.updateById(user);
    }

    @Override
    public void delete(String id) {
        userDao.deleteById(id);
    }



    @Override
    public PageInfo<User> findPage(String companyId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list = userDao.findAll(companyId);
        return new PageInfo<User>(list,5);
    }

    @Override //先删除再新增
    public void changeRole(String userid, String[] roleIds) {

//        先把这个用户下的角色全部删除
//                delete from pe_role_user where user_id=?
        userDao.deleteUserAndRoleByUserId(userid);
//        就是向角色用户表中插入数据 insert into pe_role_user(user_id,role_id)values(#{userid},#{roleid})
        if(roleIds!=null&&roleIds.length>0){
            for (String roleid : roleIds) {
                userDao.saveUserAndRole(userid,roleid);
            }
        }
    }

    @Override
    public List<String> findRoleIdsByUserId(String userid) {
        return userDao.findRoleIdsByUserId(userid);
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public List<User> findbycurrentDate(String currentDate) {
        return userDao.findbycurrentDate(currentDate);
    }

    @Override
    public List<User> findByDate(String newDate) {
        return userDao.findByDate( newDate);
    }

    public static void main(String[] args) {
        System.out.println(new Md5Hash("123456", "laowang@export.com", 2).toString());//035acbae4c75daf7f729c86fde0cf45c
        System.out.println(new Md5Hash("123456", "xiaowang@export.com", 2).toString());//b9624cad1a4c522e9d458f42c6c6183f
        System.out.println(new Md5Hash("123456", "admin@export.com", 2).toString());//315c682616a05c898f72a0ddd614efd9
        System.out.println(new Md5Hash("123456", "xiaoer@export.com", 2).toString());//359c4cd72b2c243b64371da49ef82d75
        System.out.println(new Md5Hash("123456", "dawang@export.com", 2).toString());//359c4cd72b2c243b64371da49ef82d75
    }

    // delete  save  update
}
